/**
 * @file Service_2.h
 *
 * @author Federico Consoli
 *
 * Released under GNU GPLv3 (http://www.gnu.org/licenses/gpl-3.0.txt)
 */

#ifndef SERVICE2_H
#define	SERVICE2_H

#include "libreria.h"

using namespace std;

/** Implementation of the Service_2 class.
 *  This class can receive, store and send image or files.
 *
 */
class Service_2 : public Response {
    /** Mutual exclusion for storage management*/
    Lettori_scrittori RW;

    /** Base Ip address of the services provider, each services will
     * increment it of 1 */
    string ip;
    /** Base port of the services provider, each services will
     * increment it of +1 so if you got 5 services and <b> porta_base </b>= 40000
     *  the port 40000, 40001, 40002 and 40004 will be used */
    unsigned short porta_base;
    /** String that contains the list of images*/
    string lista_img;

    /** Storage struct of a image */
    struct list_val {
        /** Pointer to a @link Response::valori valori @endlink struct */
        valori* elem;
        /** Pointer to next @link list_val @endlink element */
        list_val*succ;
    };
    /** Head of the list of the image */
    list_val*testa;
    /**
     * Find a image in the storage buffer
     * @param name Name of the image.
     * @return Pointer to @link Response::valori valori @endlink struct that contains the image.
     */
    valori*find_elem(string);

public:

    /**
     * Constructor of Service_2 class.
     * @param nuovo_ip @link Service_2::ip Ip @endlink of services.
     * @param nuovo_porta_base @link Service_2::porta_base Porta_base @endlink of the service
     * @param name_of_service @link Service::name name @endlink of the services
     */
    Service_2(string nuovo_ip = "127.0.0.1", unsigned short nuovo_porta_base = 31000, string name_of_service = "SERVICE 2");

    /**
     * Destroyer. Delete all the buffer storage.
     *
     */
    ~Service_2();

    /**
     * Save the buffer storage to the disk.
     */
    void salva();


    /**
     * Set the name of the Server class and display the menu
     * @param registro_locale Pointer to Server class
     */
    void menu(Server*);

    /**
     * Get an image from the client and store it.
     * @param R Response pointer.
     * @param sd Socket descriptor of the connection to the @link Client client@endlink.
     * @return 0
     */
    int store(Response*, int);

    /**
     * Send an image to the client
     * @param R Response pointer.
     * @param sd Socket descriptor of the connection to the @link Client client@endlink.
     * @return 0 if the image is present else 1.
     */
    int get_img(Response*, int);

    /**
     * Send the list image to the client
     * @param sd Socket descriptor of the connection to the @link Client client@endlink.
     * @return 0
     */
    int get_list(int);
};

#endif	/* SERVICE2_H */

